#!/bin/bash
#this script run for centos6.x 64bit

dir='/data/opt'
DB_ROOT_PW='aOS327*&^EE1Zh2016'

nginx='nginx-1.7.0.tar.gz'
php='php-5.3.29.tar.bz2'
mysql='mysql-5.5.39.tar.gz'
libiconv='libiconv-1.13.1.tar.gz'
libmcrypt='libmcrypt-2.5.8.tar.gz'
mcrypt='mcrypt-2.6.8.tar.gz'
memcache='memcache-2.2.6.tgz'
PDO_MYSQL='PDO_MYSQL-1.0.2.tgz'
mhash='mhash-0.9.9.9.tar.gz'
pcre='pcre-8.20.zip'
cmake='cmake-2.8.6.tar.gz'
memcached='memcached-1.4.5.tar.gz'
libevent='libevent-2.0.11-stable.tar.gz'
mycnf='my.cnf'
php_fpm='php-fpm.conf'
nginxconf='nginx.conf'
libhiredis='hiredis-0.13.1.tar.gz'
liblua='lua-5.2.3.tar.gz'
libprotobuf='protobuf-2.5.0.tar.bz2'

cpunum=`cat /proc/cpuinfo | grep "processor" | wc -l`

function f_check(){
	if [ $? -ne 0 ]
	then
		echo -e "\033[0;33;1m########################################################\033[0m"
		echo -e "\033[0;31;1mInstall error!~~~\033[0m"
		echo -e "\033[0;31;1mInput '1' to Try Agin,while you fixed the error.\033[0m"
		echo -e "\033[0;31;1mInput '2' to exit.\033[0m"
		echo -e "\033[0;31;1mor press 'enter' to continue\033[0m"
		echo -e "\033[0;33;1m########################################################\033[0m"
		read -p "Select:" a
		case $a in
			1)
			$1
			;;
			2)
			exit 2;
			;;
			*)
			continue
			;;
		esac
	fi
}

function f_cutname(){
	cd $dir
	filename=`basename $1`
	foldername=`echo $filename | sed 's/\.tar\.gz//' | sed 's/\.tar\.bz2//' | sed 's/\.tgz//' | sed 's/\.zip//'` 
	type=`echo $filename | awk -F . '{printf $NF}'`
	#if [ -f $filename ]
	#then
		#rm -rf $filename
	#fi
	if [ -d $foldername ]
	then
		rm -rf $foldername
	fi
	#wget $1
	case $type in
		gz|tgz)
		tar zxvf $filename
		;;
		bz2)
		tar jxvf $filename
		;;
		zip)
		unzip $filename
		;;
		*)
		echo $filename Type error!
		exit
		;;
	esac
	cd $foldername
	echo $foldername
}

function f_yum_lib(){
#/usr/bin/yum clean all
#/usr/bin/yum -y update
/usr/bin/yum -y install vim lrzsz readline-devel
/usr/bin/yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers wget perl-CPAN lrzsz unzip vim* openssh-clients dos2unix rsync
}

function f_libiconv_install(){
        f_cutname $libiconv
        ./configure --prefix=/usr/local --enable-static=yes
        f_check  $FUNCNAME
        make -j $cpunum;make install
        f_check  $FUNCNAME
}

function f_libhiredis_install(){
		f_cutname $libhiredis
		make clean;make;make install
		f_check $FUNCNAME		
}

function f_liblua_install(){
		f_cutname $liblua
		make clean;make linux;make install
		f_check $FUNCNAME
}

function f_libprotobuf_install(){
		f_cutname $libprotobuf
		 ./configure --prefix=/usr/local
        f_check  $FUNCNAME
        make clean;make -j $cpunum;make install
        f_check $FUNCNAME
}

function f_libmcrypt_install(){
	f_cutname $libmcrypt
	./configure
	make;make install
	/sbin/ldconfig
	cd libltdl
	./configure --enable-ltdl-install
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
	cd /usr/local/lib/
	for i in `ls libmcrypt.*`
	do
		ln -sv /usr/local/lib/"$i" /usr/lib/"$i" 
	done
	ln -sv /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
}

function f_mhash_install(){
        f_cutname $mhash
	./configure
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
	cd /usr/local/lib/
	for i in `ls libmhash.*`
	do
		ln -sv /usr/local/lib/"$i" /usr/lib/"$i"
	done
}

function f_mcrypt_install(){
	f_cutname $mcrypt
	/sbin/ldconfig
	./configure
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
}

function f_cmake_install(){
	f_cutname $cmake
	./bootstrap
        f_check  $FUNCNAME
	gmake;make install
        f_check  $FUNCNAME
}

function f_mysql_install(){
	/usr/sbin/groupadd mysql
	/usr/sbin/useradd -g mysql mysql
	f_cutname $mysql
	cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 \
		-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
		-DENABLED_LOCAL_INFILE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci  -DEXTRA_CHARSETS=all \
		-DMYSQL_TCP_PORT=3306 -DWITH_DEBUG=OFF -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
	ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
	if [ -d /usr/lib64 ];then
		ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
	fi
}

function f_mysql_management(){
	killall mysql
	#wget $mycnf
	/bin/cp -rf "$dir/$mycnf" .
	mkdir -p /data/mysql
	mkdir -p /data/mysqlbinlog
	chown mysql:mysql /data/mysql
	chown mysql:mysql /data/mysqlbinlog
	/bin/cp -Rp my.cnf /etc/my.cnf
	chmod 755 scripts/mysql_install_db
	scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
	/bin/cp -Rp support-files/mysql.server /etc/init.d/mysql
	chmod 755 /etc/init.d/mysql
	chkconfig mysql on
	service mysql start
	/usr/local/mysql/bin/mysqladmin -uroot password $DB_ROOT_PW	
	/usr/local/mysql/bin/mysql -uroot -p'aOS327*&^EE1Zh2016' -e "grant all privileges on *.* to root@'%' Identified by 'aOS327*&^EE1Zh2016'"
	/usr/local/mysql/bin/mysql -uroot -p'aOS327*&^EE1Zh2016' -e "grant all privileges on *.* to root@'127.0.0.1' Identified by 'aOS327*&^EE1Zh2016'"
}

function f_php_install(){
	ln -sv /usr/lib64/libldap* /usr/lib/
	f_cutname $php
	./configure 	--prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql \
		--with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir \
		--with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath \
		--enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization \
		--with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt \
		--with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets \
		--with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-pdo-mysql=/usr/local/mysql
        f_check  $FUNCNAME
	make ZEND_EXTRA_LIBS='-liconv' -j $cpunum;make install
        f_check  $FUNCNAME
	cp php.ini-production /usr/local/php/etc/php.ini
	sed -i 's@; output_buffering@output_buffering=on@' /usr/local/php/etc/php.ini
	sed -i 's@;cgi.fix_pathinfo=1@cgi.fix_pathinfo=0@' /usr/local/php/etc/php.ini
	sed -i 's@; extension_dir = "./"@extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626"@' /usr/local/php/etc/php.ini
	sed -i 's@short_open_tag = Off@short_open_tag = On@' /usr/local/php/etc/php.ini
	sed -i 's/xpose_php = On/xpose_php = Off/' /usr/local/php/etc/php.ini
	#wget $php_fpm
	/bin/cp -rf "$dir/$php_fpm" .
	/bin/cp -Rp php-fpm.conf /usr/local/php/etc/php-fpm.conf
	#add to chkconfig:
	/bin/cp -a sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
	chmod 775 /etc/init.d/php-fpm
	chkconfig --add php-fpm
	#sed -i '/php-fpm/d' /etc/rc.d/rc.local
	#echo "/usr/local/php/sbin/php-fpm" >> /etc/rc.d/rc.local
}

function f_memcache_install(){
	f_cutname $memcache
	/usr/local/php/bin/phpize
	./configure --with-php-config=/usr/local/php/bin/php-config
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
	sed -i '810aextension = "memcache.so"' /usr/local/php/etc/php.ini
}

function f_PDO_MYSQL_install(){
	f_cutname $PDO_MYSQL
	/usr/local/php/bin/phpize
	./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
	sed -i '810aextension = "pdo_mysql.so"' /usr/local/php/etc/php.ini
}

function f_pcre_install(){
	f_cutname $pcre
	./configure
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
}

function f_nginx_install(){
	mkdir -p /data/www
	mkdir -p /data/nginx_logs
	f_cutname $nginx
	/usr/sbin/groupadd www
	/usr/sbin/useradd -m www -g www -s /sbin/nologin -d /usr/local/nginx
	chown -R www:www /data/www
	chown -R www:www /data/nginx_logs
	./configure  --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module \
		--user=www --group=www --with-http_realip_module --with-http_flv_module --with-http_gzip_static_module
        f_check  $FUNCNAME
	make -j $cpunum;make install
        f_check  $FUNCNAME
	mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
	#wget $nginxconf
	/bin/cp -rf "$dir/$nginxconf" .
	/bin/cp -Rp nginx.conf /usr/local/nginx/conf/nginx.conf
	mkdir -p /usr/local/nginx/conf/vhost
        #wget http://119.146.200.150:37000/download/conf_nginx/domain.conf -O /usr/local/nginx/conf/vhost/domain.conf
	#add to chkconfig:
	#wget http://119.146.200.150:37000/config/nginx -O /etc/init.d/nginx
	/bin/cp -rf "$dir/nginx" /etc/init.d/nginx
	chmod +x /etc/init.d/nginx
	chkconfig --add nginx

cat > /usr/local/nginx/conf/fcgi.conf << "EOF"
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
EOF
#sed -i '/nginx/d' /etc/rc.d/rc.local
#echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
}

function f_libevent_install(){
	f_cutname $libevent
	./configure
	f_check  $FUNCNAME
	make -j $cpunum;make install
	f_check  $FUNCNAME
	ln -sv /usr/local/lib/libevent-2.0.so.5 /usr/lib/
	if [ -d /usr/lib64 ];then
		ln -sv /usr/local/lib/libevent-2.0.so.5 /usr/lib64/
	fi
}


function f_memcached_install(){
	f_cutname $memcached
	./configure --prefix=/usr/local/memcached --with-libevent=/usr
	f_check  $FUNCNAME
	make -j $cpunum;make install
	f_check  $FUNCNAME
	sed -i '/memcached/d' /etc/rc.d/rc.local
	echo "/usr/local/memcached/bin/memcached -d -m 256 -p 11211 -u www 127.0.0.1" >> /etc/rc.d/rc.local
}

function f_sysctl(){
cat >> /etc/sysctl.conf << "EOF"
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 4096  65535
kernel.shmmax = 2147483648
EOF

/sbin/sysctl -p
}

f_rsa_id_install(){
		if [ ! -d /home/dgkang/.ssh ];then
			mkdir -p /home/dgkang/.ssh
			chown dgkang:dgkang /home/dgkang/.ssh
		fi
		cd $dir
		cat id_rsa >> /home/dgkang/.ssh/id_rsa
		chown dgkang:dgkang /home/dgkang/.ssh/id_rsa
		chmod 600 /home/dgkang/.ssh/id_rsa
}

function f_epel_install(){
		cd $dir
		rpm -ivh epel-release-6-8.noarch.rpm
		rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
		\cp ./epel.repo /etc/yum.repos.d/epel.repo
		chmod 644 /etc/yum.repos.d/epel.repo
}

function f_initialization()
{
cd $dir
#wget $yum163
#sh 163yum_for_6.3.sh

#sed -i '/ulimit/d' /etc/rc.d/rc.local
#echo "ulimit -SHn 65535" >> /etc/rc.d/rc.local
#cat >> /etc/security/limits.conf << EOF
#*	soft	nofile	65535
#*	hard	nofile	65535
#EOF

#yum -y install ntp
#/usr/sbin/ntpdate  
#sed -i '/ntpdate/d' /var/spool/cron/root
#echo "0 4 * * * /usr/sbin/ntpdate ntp.api.bz > /dev/null 2>&1" >> /var/spool/cron/root

#sed -i "8i alias vi='vim'" /root/.bashrc
#echo 'syntax on' > /root/.vimrc 
#sed -i "9i alias n='netstat -tunlp'" /root/.bashrc
#. /root/.bashrc

#close ipv6
#echo "alias net-pf-10 off" >> /etc/modprobe.conf
#echo "alias ipv6 off" >> /etc/modprobe.conf
#/sbin/chkconfig --level 35 ip6tables off

#close selinux
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config 
/usr/sbin/setenforce 0

#close services
#for i in `ls /etc/rc3.d/S*`
#do
# CURSRV=`echo $i|cut -c 15-`
# echo $CURSRV
# case $CURSRV in
#           crond | irqbalance | microcode_ctl | lvm2-monitor | network | random | sshd | rsyslog  )
#       echo "Base services, Skip!"
#       ;;
#       *)
#           echo "change $CURSRV to off"
#           chkconfig --level 235 $CURSRV off
#           service $CURSRV stop
#       ;;
# esac
#done

#ssh
#sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
#sed -i 's/^#UseDNS yes$/UseDNS no/' /etc/ssh/sshd_config
#/sbin/service sshd restart

#echo "$ROOT_PW" | passwd root --stdin


#if [ ! -d /root/.ssh ];then
#	mkdir -p /root/.ssh
#	chmod 700 /root/.ssh
#fi

#wget $mykey -O /root/.ssh/authorized_keys
#chmod 600 /root/.ssh/authorized_keys

#cd /root
#wget $iptable -O iptables.sh
#chmod +x iptables.sh
#sed -i '/iptables/d' /etc/rc.d/rc.local
#echo "/$dir/iptables.sh" >> /etc/rc.d/rc.local
sh ./iptables.sh
}



#main:
starttime=`date +%s`
#判断域名解析： 
#ping -c1 163.com
#if [ $? -ne 0 ]
#then
#	echo "DNS Error!"
#	exit 2
#fi
#安装软件：

f_initialization
f_epel_install
f_rsa_id_install
f_yum_lib
f_libiconv_install
f_libmcrypt_install
f_mhash_install
f_mcrypt_install
f_cmake_install
f_mysql_install
f_mysql_management
f_php_install
f_memcache_install
f_pcre_install
f_nginx_install
f_liblua_install
f_libhiredis_install
f_libprotobuf_install


/sbin/service php-fpm restart
/sbin/service nginx restart
endtime=`date +%s`
echo "This scripts use $((endtime - starttime)) s!"

echo "Please reboot now!"


